<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>indexDB.html</title>
</head>
<body>
    <script>
        // var a = 2;
        // const request = 1;
        // index 索引
        var db;
        var request = window.indexedDB.open('blogs',2);
        // console.log(request);
        request.onerror = function(event) {
            console.log('数据库打开失败');
        }
        request.onsuccess = function(event) {
            db = request.result;
            create();
        }
        request.onupgradeneeded = function() {
            db = request.result;
            var objectStore;
            if(!db.objectStoreNames.contains('person')) {
                // 索引就是为了更快查询 doc(id) 详情页 加一个索引
                // 列表 .get() .skip() limit()
                // 详情页 /detail?id= + ${'id'}
                objectStore = db.createObjectStore('person',{keyPath:'id'});
                // /search?name = %xiaoLang%
                objectStore.createIndex('name','name',{unique:false});
                objectStore.createIndex('email','email',{unique:true})
            }
        }
          function create() {
            var request = transaction(['person'],'readwrite')
                .objectStore('person') 
                .add({id:1,name:'xht',email:'xt@tmall.com'})
            request.onsuccess = function() {
                console.log('数据写入成功');
            }
            request.onerror = function() {
                console.log('数据写入失败');
            }
        }
    </script>
</body>
</html>